package biz;

import dao.EmployeeDao;

import java.sql.Date;
import java.util.Map;

public class EmployeeBiz {

    private EmployeeDao dao = new EmployeeDao();

    public void check(String name, String position, String entryTime, String salary, String department, String telphone) throws BizException {
        if (name.length() < 2 || name.length() > 16){
            throw new BizException("姓名只能由2~16位中文或英文组成");
        }
        if (!name.matches("^[a-zA-Z\\u4e00-\\u9fa5]+$")){
            throw new BizException("姓名只能由2~16位中文或英文组成");
        }
        System.out.println("position:" + position);
        System.out.println(position.length());
        if (position.length() < 2 || position.length() > 6){
            throw new BizException("职位名由2~6位中文组成");
        }
        if (entryTime.matches("\\d{4}-\\d{1,2}-\\d{1,2}") == false){
            throw new BizException("入职日期格式不正确");
        }
        if (salary == null || salary.trim().isEmpty()){
            throw new BizException("薪资不能为空!");
        }
        if (department.length() < 2 || department.length() >6){
            throw new BizException("部门名由2~6位中文组成");
        }
        if (telphone.length() < 2 || telphone.length() > 20){
            throw new BizException("电话号码要求2~20位");
        }
        if (telphone.matches("\\D+")){
            throw new BizException("电话号码只能是数字组成!");
        }

    }

    public Map<String, Object> query(String position, String department, int page, int size){
        return dao.query(position, department, page, size);
    }

    public int existsEmp(String name, String email){
        return dao.existsEmp(name, email);
    }

    public void create(String name, String position, String entryTime, String salary, String department, String telphone) throws BizException {
        check(name, position, entryTime, salary, department, telphone);
        if (dao.exists(telphone,name) == 1){
            // 如果存在 则不允许添加
            throw new BizException("该员工已存在!");
        }
        dao.create(name, position, entryTime, salary, department, telphone);
    }

    public void modify(String id, String name, String position, String entryTime, String salary, String department, String telphone) throws BizException {
        check(name, position, entryTime, salary, department, telphone);
        dao.modify(id, name, position, entryTime, salary, department, telphone);
    }
    public void remove(String id){
        dao.remove(id);
    }
    public void remove1(String id){
        dao.remove1(id);
    }

    public void modifyPwd(String name, String password, String email) throws BizException {
        if (name == null || name.trim().isEmpty()){
            throw new BizException("用户名不能为空");
        }
        dao.modifyPwd(name, password, email);
    }


}
